package com.whw.springboot.walk.controller;

import com.whw.springboot.walk.UseToken;
import com.whw.springboot.walk.entity.Lesson;
import com.whw.springboot.walk.entity.LessonNumber;
import com.whw.springboot.walk.entity.Message;
import com.whw.springboot.walk.entity.UserInfo;
import com.whw.springboot.walk.service.LessonApplyService;
import com.whw.springboot.walk.service.LessonService;
import com.whw.springboot.walk.service.UserInfoService;
import com.whw.springboot.walk.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.List;

@Api(tags = "课程模块")
@RestController
@RequestMapping("/api/lesson")
public class LessonController {

    @Resource
    private LessonService lessonService;
    @Resource
    private UserInfoService userInfoService;

    @ApiOperation(value = "管理端增加课程信息")
    @PostMapping(value = "/insertLesson")
    @UseToken(level = 1)
    public Message addLesson(LessonNumber lessonNumber) throws ParseException {
        System.out.println(lessonNumber.toString());
        return lessonService.addLesson(lessonNumber);
    }


    @ApiOperation(value = "管理端根据课程ID删除课程信息")
    @DeleteMapping(value = "/deleteLessonByPrimaryKey")
    @UseToken(level = 1)
    public Message deleteByPrimaryKey(String classId) {
        return lessonService.deleteLessonByPrimaryKey(classId);
    }


    @ApiOperation(value = "查询所有课程名字")
    @GetMapping(value = "/selectAllLessonName")
    public Message<List<Lesson>> selectAllLessonName() {
        return lessonService.selectAllLessonName();
    }


    @ApiOperation(value = "查看所有授课教师")
    @GetMapping(value = "/selectAllTeacher")
    public Message<List<Lesson>> selectAllTeacher() {
        return lessonService.selectAllTeacher();
    }


    @ApiOperation(value = "根据课程名字查询授课教师")
    @GetMapping(value = "/selectTeacherByLessonName")
    public Message<Lesson> selectTeacherByLessonName(String lessonName) {
        return lessonService.selectTeacherByLessonName(lessonName);
    }


    @ApiOperation(value = "根据授课教师查询课程名字")
    @GetMapping(value = "/selectLessonNameByTeacher")
    public Message<Lesson> selectLessonNameByTeacher(String lessonTeacher) {
        return lessonService.selectLessonNameByTeacher(lessonTeacher);
    }


    @ApiOperation(value = "管理端根据课程名字设置旁听人数")
    @PutMapping(value = "/updateLessonPersonNumByLessonName")
    @UseToken(level = 1)
    public Message updateLessonPersonNumByLessonName(LessonNumber lessonNumber) {
        return lessonService.updateLessonPersonNumByLessonName(lessonNumber);
    }


    @ApiOperation(value = "管理端根据主键设置旁听人数")
    @PutMapping(value = "/updateLessonPersonNumByPK")
    @UseToken(level = 1)
    public Message updateLessonPersonNumByPK(String lessonId, String lessonPersonNum) {
        return lessonService.updateLessonPersonNumByPK(lessonId, lessonPersonNum);
    }

    @ApiOperation(value = "根据课程编号查看课程详情")
    @GetMapping(value = "/selectLessonByLessonId")
    public Message selectLessonByLessonId(String lessonId) {
        return lessonService.selectLessonByLessonId(lessonId);
    }


    @ApiOperation(value = "根据课程名称查询用户信息")
    @GetMapping(value = "/selectUserInfoByLessonName")
    @UseToken(level = 1)
    public Message<List<UserInfo>> findUserInfoByLessonName(String lessonName) {
        return userInfoService.findUserInfoByLessonName(lessonName);
    }


    /**
     * 导入excel
     */
    @RequestMapping("/import")
    @ResponseBody
    public String excelImport(@RequestParam(value = "filename") MultipartFile file, HttpSession session) {
//		String fileName = file.getOriginalFilename();
        int result = 0;
        try {
            result = lessonService.importLesson(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (result > 0) {
            return "excel文件数据导入成功！";
        } else {
            return "excel数据导入失败！";
        }

    }

}

